package com.prs.crm.service.hr.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.hr.PayrollDao;
import com.prs.crm.domain.hr.Payroll;
import com.prs.crm.domain.hr.PayrollBill;
import com.prs.crm.domain.hr.PayrollKpi;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.SalaryFloatItem;
import com.prs.crm.domain.hr.SalaryRelat;
import com.prs.crm.domain.oa.JobPlan;
import com.prs.crm.service.hr.PayrollBillService;
import com.prs.crm.service.hr.PayrollKpiService;
import com.prs.crm.service.hr.PayrollService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.SalaryFloatItemService;
import com.prs.crm.service.hr.SalaryItemService;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.oa.JobPlanService;

public class PayrollServiceImpl extends BaseServiceImpl<Payroll,PayrollDao> implements PayrollService{
	private PersonService personService;
	private PayrollBillService payrollBillService;
	private SalaryItemService salaryItemService;
	private SalaryFloatItemService salaryFloatItemService;
	private PayrollKpiService payrollKpiService;
	private JobPlanService jobPlanService;
	
	@Override
	public void savePayroll(Payroll payroll,String issuePerson,PayrollKpi payrollKpi) {
		// TODO Auto-generated method stub		
		payroll.setState(Constrains.PAYROLL_EDIT);		
		this.getDao().save(payroll);
		
		if(!payrollKpi.getKpiType().equals("请选择")) {
			payrollKpi.setPayroll(payroll);
		}

		List<SalaryFloatItem> floatItems = salaryFloatItemService.getAllSalaryFloatItem();
		Collection<Person> persons = personService.findPersons(ToolKits.convertStringAryToIntAry(issuePerson.split(",")));
		Iterator<Person> iter = persons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			Double totalMoney = 0.0;
			String jobPlanType ="";
		/*	if(!payrollKpi.getKpiType().equals("请选择")) {
				if(payrollKpi.getKpiType().equals("周计划标准")) {
					jobPlanType = Constrains.JOBPLAN_WEEKLYPLAN;
					System.out.println(jobPlanType);
				}else if(payrollKpi.getKpiType().equals("日计划标准")) {
					jobPlanType = Constrains.JOBPLAN_DAILYPLAN;
				}else if(payrollKpi.getKpiType().equals("月计划标准")) {
					jobPlanType = Constrains.JOBPLAN_MONTHLYPLAN;
				}else if(payrollKpi.getKpiType().equals("任意期计划标准")) {
					jobPlanType = Constrains.JOBPLAN_WILFULLYPLAN;
				}
				Double totalScores = 0.0;
				Collection<JobPlan> jobPlans = this.getJobPlanService().getJobPlanByKpi(payroll.getIssueYear(), payroll.getIssueMonth(), jobPlanType, person);
				for(JobPlan jobPlan : jobPlans) {
					totalScores += jobPlan.getJobPlanAssess().getScores();
				}
				totalMoney = totalScores * payrollKpi.getTally();
			}
			*/
			Collection<SalaryRelat> relats = person.getSalary().getRelats();
			Iterator<SalaryRelat> iterRelat = relats.iterator();
			while(iterRelat.hasNext()) {
				SalaryRelat relat = iterRelat.next();
				Double defaultMoney = relat.getValue();
				
				if(!payrollKpi.getKpiType().equals("请选择") &&payrollKpi.getSalaryItem() != null) {
					if(relat.getSalaryItem().getId().intValue() == payrollKpi.getSalaryItem().getId().intValue()) {
						defaultMoney += totalMoney;
					}
				}
				
				PayrollBill bill = new PayrollBill();	
				bill.setPerson(person);
				bill.setPayroll(payroll);			
				bill.setState(Constrains.PAYROLL_EDIT);				
				bill.setItem(relat.getSalaryItem().getName());
				bill.setValue(defaultMoney);
				bill.setType(relat.getSalaryItem().getType());
				
				this.getPayrollBillService().save(bill);
			}
			
			Iterator<SalaryFloatItem> iterFloat = floatItems.iterator();
			while(iterFloat.hasNext()) {
				SalaryFloatItem item = iterFloat.next();
				Double defaultMoney = 0.0;
				
				if(!payrollKpi.getKpiType().equals("请选择") &&payrollKpi.getSalaryFloatItem() != null) {
					if(item.getId().intValue() == payrollKpi.getSalaryFloatItem().getId().intValue()) {
						defaultMoney = totalMoney;
					}
				}
				
				PayrollBill bill = new PayrollBill();	
				bill.setPerson(person);
				bill.setPayroll(payroll);			
				bill.setState(Constrains.PAYROLL_EDIT);
				bill.setItem(item.getName());
				bill.setValue(defaultMoney);
				bill.setType(item.getType());
				
				this.getPayrollBillService().save(bill);
			}	
		}
	}
	
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPayrollBillService(PayrollBillService payrollBillService) {
		this.payrollBillService = payrollBillService;
	}
	public PayrollBillService getPayrollBillService() {
		return payrollBillService;
	}
	public SalaryItemService getSalaryItemService() {
		return salaryItemService;
	}
	public void setSalaryItemService(SalaryItemService salaryItemService) {
		this.salaryItemService = salaryItemService;
	}
	public SalaryFloatItemService getSalaryFloatItemService() {
		return salaryFloatItemService;
	}
	public void setSalaryFloatItemService(
			SalaryFloatItemService salaryFloatItemService) {
		this.salaryFloatItemService = salaryFloatItemService;
	}

	@Override
	public Collection<Payroll> getAllPayroll() {
		// TODO Auto-generated method stub
		return this.getDao().getAllPayroll();
	}

	@Override
	public Collection<Payroll> getPayrollByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.getDao().getPayrollByIds(ToolKits.convertToIntAry(ids));
	}

	@Override
	public Collection<Payroll> getPayrollByPerson(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getPayrollByPerson(person);
	}

	public void setPayrollKpiService(PayrollKpiService payrollKpiService) {
		this.payrollKpiService = payrollKpiService;
	}

	public PayrollKpiService getPayrollKpiService() {
		return payrollKpiService;
	}

	public void setJobPlanService(JobPlanService jobPlanService) {
		this.jobPlanService = jobPlanService;
	}

	public JobPlanService getJobPlanService() {
		return jobPlanService;
	}

}
